package com.example.zhunongzonghe.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.zhunongzonghe.beans.ZnShoppingcartBean;
import com.example.zhunongzonghe.common.Result;
import com.example.zhunongzonghe.entity.ZnShoppingcart;
import com.example.zhunongzonghe.service.IZnOrderDetailsService;
import com.example.zhunongzonghe.service.IZnOrderService;
import com.example.zhunongzonghe.service.IZnShoppingcartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.security.Principal;
import java.time.LocalDateTime;
import java.util.List;


@RestController
@RequestMapping("/znShoppingcart")
public class ZnShoppingcartController{
    @Autowired
    private IZnShoppingcartService znShoppingcartService;
    @Autowired
    private IZnOrderService iZnOrderService;
    @Autowired
    private IZnOrderDetailsService iZnOrderDetailsService;
    /**
     * 添加购物车
     * @param znShoppingcartBean
     * @param principal
     * @return
     */
    @PostMapping("/AppendCart")
    public Result AppendCart(@RequestBody ZnShoppingcartBean znShoppingcartBean,Principal principal){
        LambdaQueryWrapper<ZnShoppingcart> queryWrapper =new LambdaQueryWrapper<>();
        queryWrapper.eq(ZnShoppingcart::getSpecId,znShoppingcartBean.getSpecId())
                .eq(ZnShoppingcart::getUsername,principal.getName());
        ZnShoppingcart shoppingcart = znShoppingcartService.getOne(queryWrapper);
        if (shoppingcart!=null){
            return new Result<>(false,"该规格已加购，请选择其他规格",400);
        }else {
            ZnShoppingcart znShoppingcart = new ZnShoppingcart();
            znShoppingcart.setFarmId(znShoppingcartBean.getFarmId());
            znShoppingcart.setSpecId(znShoppingcartBean.getSpecId());
            znShoppingcart.setUsername(principal.getName());
            znShoppingcart.setCount(znShoppingcartBean.getCount());
            znShoppingcart.setCreateTime(LocalDateTime.now());
            znShoppingcart.setUpdateTime(LocalDateTime.now());
            znShoppingcartService.save(znShoppingcart);
            return new Result<>(true,"添加成功",200);
        }
    }

    /**
     *
     * 当前用户购物车列表
     * @param principal
     * @return
     */
    @GetMapping("/showCart")
    public Result<List<ZnShoppingcartBean>> showCart(Principal principal){
        System.out.println("昵称为："+principal.getName());
        List<ZnShoppingcartBean> beanList = znShoppingcartService.showCart(principal.getName());
        return new Result<>(true,"查询成功",200,beanList);
    }

    /**
     * 购物车商品删除
     * @param shopId
     * @return
     */
    @DeleteMapping("/DeleteCartFarm/{shopId}")
    public Result<ZnShoppingcart> DeleteCartFarm(@PathVariable Integer shopId){
        znShoppingcartService.removeById(shopId);
        return new Result<>(true,"删除成功",200);
    }

    /**
     * 数量加减
     * @param shopId
     * @param count
     * @return
     */
    @PutMapping("update/{shopId}/{count}")
    public Result<ZnShoppingcart> updateCont(@PathVariable Integer shopId,@PathVariable Integer count){
        ZnShoppingcart znShoppingcart = znShoppingcartService.getById(shopId);
        znShoppingcart.setCount(count);
        znShoppingcartService.updateById(znShoppingcart);
        return new Result<>(true,"成功",200);
    }

    /**
     * 提交订单
     * @param znShoppingcartBean
     * @return
     */
    @PostMapping("/commitOrder")
    public Result<ZnShoppingcartBean> commitOrder(@RequestBody ZnShoppingcartBean znShoppingcartBean,Principal principal){
        System.out.println("用户名为："+znShoppingcartBean.getUsername());
        znShoppingcartService.commitOrder(znShoppingcartBean,principal);
        System.out.println("----------------"+znShoppingcartBean);
        return new Result<>(true,"提交成功",200);
    }
}